//
//  DKOrderDetailsFootView.m
//  DanKU
//
//  Created by EDY on 2023/11/6.
//

#import "DKOrderDetailsFootView.h"
#import "DKBillsChannesViewController.h"

@interface DKOrderDetailsFootView()

@property(nonatomic,strong)UIButton *dk_OrderApplyBtn;
@property(nonatomic,strong)UIButton *dk_OrderRepayBtn;
@property(nonatomic,strong)UIImageView *dk_OrderRepayChoseImg;
@property(nonatomic,strong)UILabel *dk_OrderRepayChoseLab;

@end

@implementation DKOrderDetailsFootView

-(instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame: frame]) {
        self.backgroundColor =  DK_Hex(@"#FAFAFA");
        [self creatUI];
    }
    return self;
}

-(void)setDataSoure:(DKOrderDetailModel *)dataSoure{
    _dataSoure = dataSoure;
    if (_dataSoure.delayStatus == 0) {
        self.dk_OrderApplyBtn.hidden = YES;
        self.dk_OrderRepayChoseImg.hidden = YES;
        self.dk_OrderRepayChoseLab.hidden = YES;
        [self.dk_OrderRepayBtn mas_updateConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(@(DK_IsWidthScale(30)));
            make.right.equalTo(@(-DK_IsWidthScale(30)));
            make.top.equalTo(self).offset(DK_IsWidthScale(50));
            make.height.equalTo(@(DK_IsWidthScale(50)));
        }];
    }
}


-(void)creatUI{
    [self addSubview:self.dk_OrderApplyBtn];
    [self addSubview:self.dk_OrderRepayBtn];
    [self addSubview:self.dk_OrderRepayChoseImg];
    [self addSubview:self.dk_OrderRepayChoseLab];
    
    [self.dk_OrderApplyBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(DK_IsWidthScale(30)));
        make.right.equalTo(@(-DK_IsWidthScale(30)));
        make.top.equalTo(self).offset(DK_IsWidthScale(50));
        make.height.equalTo(@(DK_IsWidthScale(50)));
    }];
    
    [self.dk_OrderRepayBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.height.equalTo(self.dk_OrderApplyBtn);
        make.right.equalTo(@(-DK_IsWidthScale(30)));
        make.top.equalTo(self.dk_OrderApplyBtn.mas_bottom).offset(DK_IsWidthScale(10));
    }];
    
    [self.dk_OrderRepayChoseImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(30);
        make.width.mas_equalTo(150);
        make.right.equalTo(self.dk_OrderApplyBtn).offset(-20);
        make.bottom.equalTo(self.dk_OrderApplyBtn.mas_top).offset(10);
    }];
    
    [self.dk_OrderRepayChoseLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.dk_OrderRepayChoseImg);
    }];
    
    UIButton * dk_HomeAgreementBtn = [[UIButton alloc] init];
    [dk_HomeAgreementBtn setTitle:@"《Privacy Agreement》" forState:UIControlStateNormal];
    [dk_HomeAgreementBtn setTitleColor:DK_Hex(@"#CCCCCC") forState:UIControlStateNormal];
    dk_HomeAgreementBtn.titleLabel.font = DK_FONT_Regular(12);
    [dk_HomeAgreementBtn addTarget:self action:@selector(homeCheckAgreement) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:dk_HomeAgreementBtn];
    [dk_HomeAgreementBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self);
        make.top.equalTo(self.dk_OrderRepayBtn.mas_bottom).offset(DK_IsWidthScale(30));
        make.height.equalTo(@(DK_IsWidthScale(17)));
    }];
}


-(void)applyAction:(UIButton*)sender{
    DKBillsChannesViewController *vc = [DKBillsChannesViewController new];
    vc.dataSoure = self.dataSoure;
    if (sender.tag == 10) {
        vc.dkRepaymentMethod = Extension;
    }else{
        vc.dkRepaymentMethod = Normal;
    }
    
    [[self viewController].navigationController pushViewController:vc animated:YES];
}

-(void)homeCheckAgreement{
    DKWebViewController * dk_WebViewCtrl = [[DKWebViewController alloc] initWithUrl:DK_PrivacyAgreement withIsNavBar:YES];
    [self.viewController.navigationController pushViewController:dk_WebViewCtrl animated:YES];
}


-(UIButton*)dk_OrderApplyBtn{
    if (!_dk_OrderApplyBtn) {
        _dk_OrderApplyBtn = [[UIButton alloc]init];
        [_dk_OrderApplyBtn addTarget:self action:@selector(applyAction:) forControlEvents:UIControlEventTouchUpInside];
        [_dk_OrderApplyBtn setTitle:@"Apply for extension" forState:UIControlStateNormal];
        [_dk_OrderApplyBtn setTitleColor:DK_Hex(@"#FFFFFF") forState:UIControlStateNormal];
        _dk_OrderApplyBtn.titleLabel.font = DK_FONT_Semibold(16);
        _dk_OrderApplyBtn.tag = 10;
        [_dk_OrderApplyBtn setBackgroundColor:DK_Hex(@"#4566FF")];
        _dk_OrderApplyBtn.layer.cornerRadius = DK_IsWidthScale(25);
        
    }
    return _dk_OrderApplyBtn;
}

-(UIButton*)dk_OrderRepayBtn{
    if (!_dk_OrderRepayBtn) {
        _dk_OrderRepayBtn = [[UIButton alloc]init];
        [_dk_OrderRepayBtn addTarget:self action:@selector(applyAction:) forControlEvents:UIControlEventTouchUpInside];
        [_dk_OrderRepayBtn setTitle:@"Repay now" forState:UIControlStateNormal];
        [_dk_OrderRepayBtn setTitleColor:DK_Hex(@"#4566FF") forState:UIControlStateNormal];
        _dk_OrderRepayBtn.titleLabel.font = DK_FONT_Semibold(16);
        [_dk_OrderRepayBtn setBackgroundColor:DK_Hex(@"#F0F2FF")];
        _dk_OrderRepayBtn.tag = 11;
        _dk_OrderRepayBtn.layer.cornerRadius = DK_IsWidthScale(25);
        
    }
    return _dk_OrderRepayBtn;
}

- (UIImageView*)dk_OrderRepayChoseImg{
    if (!_dk_OrderRepayChoseImg) {
        _dk_OrderRepayChoseImg = [[UIImageView alloc] init];
        _dk_OrderRepayChoseImg.image =  DK_GetImage(@"Order_repay_chose");
    }
    return _dk_OrderRepayChoseImg;
}

- (UILabel *)dk_OrderRepayChoseLab {
    if (!_dk_OrderRepayChoseLab) {
        _dk_OrderRepayChoseLab = [[UILabel alloc] init];
        _dk_OrderRepayChoseLab.textColor = DK_Hex(@"#FFFFFF");
        _dk_OrderRepayChoseLab.font = [UIFont boldSystemFontOfSize:12];
        _dk_OrderRepayChoseLab.text = @"Choice of 99% people";
        _dk_OrderRepayChoseLab.textAlignment = 1;
    }
    return _dk_OrderRepayChoseLab;
}

- (UIViewController *)viewController {
    for (UIView* next = [self superview]; next; next = next.superview) {
        UIResponder *nextResponder = [next nextResponder];
        if ([nextResponder isKindOfClass:[UIViewController class]]) {
            return (UIViewController *)nextResponder;
        }
    }
    return nil;
}


@end
